import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import ctypes
import OpenGL.GL as gl
import glm
import imgui
import glfw
from glfw import _GLFWwindow as GLFWwindow
import numpy as np
import pygl
from common import App, Cube, AxesHelper, TrackballCamera, MouseButton

class TrackballApp(App):
    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        gl.glEnable(gl.GL_DEPTH_TEST)
        self.background_color = glm.vec4(0.3, 0.3, 0.3, 1.0)
        self.cube = Cube(tex_file="textures/cratetex.png")
        self.cube_axes = AxesHelper(0.4, 5.0)
        self.world_axes = AxesHelper(0.9, 2.0)
        self.camera = TrackballCamera(45, float(self.width)/self.height, 1.0, 
                    10.0, glm.vec3(0.0, 0.0, 3.0), glm.vec3(0.0, 0.0, 0.0), 
                    glm.vec3(0.0, 1.0, 0.0))
        self.camera.setWindowSize(self.width, self.height)
        self.imgui_window_width = 250
        self.imgui_window_height = 300

    # 配置imgui界面元素
    def showImGui(self)->None:
        imgui.begin("轨迹球设置")
        imgui.text("按住鼠标左键拖动旋转立方体。")
        imgui.text("滚动鼠标滚轮可以缩放立方体。")
        imgui.text("向前滚动是放大；\n向后滚动是缩小。")
        imgui.text("按住鼠标右键拖动移动立方体")
        if imgui.button("重置"):
            self.camera.reset(glm.vec3(0.0, 0.0, 3.0), glm.vec3(0.0, 0.0, 0.0), 
                    glm.vec3(0.0, 1.0, 0.0))
        imgui.text("位置")
        imgui.begin_table("position data", 4)
        imgui.table_next_row()
        for j in range(3):
            imgui.table_next_column()
            imgui.text(str("{:.3f}").format(self.camera.position[j]))
        imgui.end_table()
        imgui.text("目标")
        imgui.begin_table("target data", 4)
        imgui.table_next_row()
        for j in range(3):
            imgui.table_next_column()
            imgui.text(str("{:.3f}").format(self.camera.center[j]))
        imgui.end_table()
        imgui.text("上向量")
        imgui.begin_table("target data", 4)
        imgui.table_next_row()
        for j in range(3):
            imgui.table_next_column()
            imgui.text(str("{:.3f}").format(self.camera.up[j]))
        imgui.end_table()
        imgui.end()       

    # 渲染图形
    def render(self) -> None:
        gl.glClearBufferfv(gl.GL_COLOR, 0, self.background_color.to_tuple())
        gl.glClearBufferfv(gl.GL_DEPTH, 0, 1.0)
        self.view = self.camera.getViewMatrix()
        self.projection = self.camera.getProjectionMatrix()
        self.cube.updateModelMatrix()
        self.cube.render(self.view,self.projection)
        self.cube_axes.model_matrix = self.cube.model_matrix
        self.cube_axes.render(self.view, self.projection)
        self.world_axes.updateModelMatrix()
        self.world_axes.render(self.view, self.projection)

    # 释放资源
    def cleanup(self) -> None:
        self.cube.delete()
        self.cube_axes.delete()
        self.world_axes.delete()

    def mouse_wheel(self, window:GLFWwindow, xoffset:float, yoffset:float)-> None:
        # 鼠标向前滚动，是放大，向后滚动，是缩小
        self.camera.zoom(yoffset)

    def mouse_move(self, window:GLFWwindow, x:float, y:float)->None:
        if glfw.get_mouse_button(self.window , glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS:
            self.camera.setMosueState(MouseButton.Left)
        elif glfw.get_mouse_button(self.window , glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS:
            self.camera.setMosueState(MouseButton.Right)
        else:
            self.camera.setMosueState(MouseButton.NONE)
        self.camera.mouseMove(x, y)

if __name__ == "__main__":
    app = TrackballApp(100, 100, 800, 800, "你好，轨迹球 App！")
    app.run()


